<?php

class SalesrepController extends Zend_Controller_Action
{

	public function indexAction()
	{
		$mapper = new Application_Model_SalesrepMapper();
		$this->view->entries = $mapper->getAll();
		$this->view->pageTitle = "View Salesrep names";
	}

	
	public function addAction()
	{
		$request = $this->getRequest();
		$form = new Application_Form_SalesRepAdd();
		$mapper = new Application_Model_SalesrepMapper();
	
		if($request->isPost()) {
			if ($form->isValid($request->getPost())) {
	
				
				$mapper->add($form->getValues());
	
				$this->_redirect('salesrep/index');
			}
		}
		$this->view->pageTitle = "Add new salesrep name";
		$this->view->form = $form;
	
	}
	
	public function editAction()
	{
		$request = $this->getRequest();
		$form = new Application_Form_SalesrepEdit();
		$mapper = new Application_Model_SalesrepMapper();
			
		if($request->isPost()) {
			if ($form->isValid($request->getPost())) {
	
				$form->getValues();
				$mapper->edit($form->getValues());
					
				$this->_redirect('salesrep/index');
			}
	
		} else {
	
			$param = $request->getParams();
			if ($param['id'] == null)
			{
				die;
			}
			else {
				$salesrep = $mapper->getSalesRepById($param['id']);
				$form->populate($salesrep);
			}
		}
			
		$this->view->pageTitle = 'Edit Salesrep';
		$this->view->form = $form;	//show form
			
	}
	
	public function deleteAction()
	{
		$mapper = new Application_Model_SalesrepMapper();
		$mapper->delete($this->getRequest()->getParam('id'));
	
		$this->_redirect('salesrep/index');
	}

}

